//
//  RPAppUpdateView.m
//  ShowYork
//
//  Created by hxh on 2020/8/25.
//  Copyright © 2020 hxh. All rights reserved.
//

typedef void(^doneBlock)(void);
typedef void(^cancelBlock)(void);
#import "RPAppUpdateView.h"
@interface RPAppUpdateView ()
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIImageView *headerImgView;
@property (nonatomic,strong)UILabel *updateContent;
@property (nonatomic,strong)UIButton *checkBtn;
@property (nonatomic,strong)UILabel *remindMessage;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)UIView *midLine;
@property (nonatomic,strong)UILabel *versionLb;
@property (nonatomic,strong)UILabel *versionTitleLb;

@property (nonatomic,strong)NSNumber *oldVersion;

@property (nonatomic,copy)doneBlock done;
@property (nonatomic,copy)cancelBlock cancel;

@property (nonatomic,strong)UIButton *forceBtn;

@property (nonatomic,strong)UpdateVersionModel *versionModel;

@end

@implementation RPAppUpdateView


- (instancetype)initWithUpdateVersionModel:(UpdateVersionModel *)versionModel completeBlock:(void (^)(void))completeBlock cancelBlock:(void (^)(void))cancelBlock{
    if (self = [super init]) {
        self.versionModel = versionModel;
        [self setUI];
        if (completeBlock) {
            self.done = ^{
                completeBlock();
            };
        }
        if (cancelBlock) {
            self.cancel = ^{
                cancelBlock();
            };
        }
    }
    return self;
}

- (void)setUI{
    
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    self.backgroundColor = RGBA(0, 0, 0, 0.3);
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor whiteColor];
    self.containView.layer.cornerRadius = 15;
    self.containView.layer.masksToBounds = YES;
    [self addSubview:self.containView];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(-44);
        make.centerX.equalTo(self);
        make.left.equalTo(self).offset(60);
        make.right.equalTo(self).offset(-60);
        
    }];
    
    
    self.headerImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_update_tag"]];
    [self.containView addSubview:self.headerImgView];
    
    [self.headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.containView);
    }];

    self.versionTitleLb = [[UILabel alloc]init];
    self.versionTitleLb.text = @"发现新版本";
    self.versionTitleLb.textColor = [UIColor whiteColor];
    self.versionTitleLb.font = [UIFont boldSystemFontOfSize:18];
    [self.containView addSubview:self.versionTitleLb];
    [self.versionTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containView).offset(24);
        make.left.equalTo(self.containView).offset(28);
    }];
    
    self.versionLb = [[UILabel alloc]init];
    self.versionLb.text = @"V1.2.0";
    self.versionLb.textColor = [UIColor whiteColor];
    self.versionLb.font = font(14);
    [self.containView addSubview:self.versionLb];
    [self.versionLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.versionTitleLb);
        make.top.equalTo(self.versionTitleLb.mas_bottom);
    }];
    
    self.updateContent = [[UILabel alloc]init];
    self.updateContent.numberOfLines = 0;
    self.updateContent.font = font(16);
    self.updateContent.textColor = RGBA(54, 54, 54, 1);
    [self.containView addSubview:self.updateContent];
    [self.updateContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containView).offset(28);
        make.right.equalTo(self.containView).offset(-16);
        make.top.equalTo(self.headerImgView.mas_bottom).offset(24);
    }];

    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn setTitle:@"下次再说" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:C102 forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(cancelMethod) forControlEvents:UIControlEventTouchUpInside];
    self.cancelBtn.layer.cornerRadius = 17;
    self.cancelBtn.titleLabel.font = font(14);
    self.cancelBtn.layer.borderColor = RGBA(221, 221, 221, 1).CGColor;
    self.cancelBtn.layer.borderWidth = 1;
    self.cancelBtn.layer.masksToBounds = YES;
    [self.containView addSubview:self.cancelBtn];

    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"立即更新" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    self.confirmBtn.titleLabel.font = font(14);
    self.confirmBtn.backgroundColor = [RPAppManager themColor];
    self.confirmBtn.layer.cornerRadius = 17;
    self.confirmBtn.layer.masksToBounds = YES;
    [self.containView addSubview:self.confirmBtn];

    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(34);
        make.left.equalTo(self.containView).offset(22);
        make.width.mas_equalTo(self.confirmBtn);
        make.top.equalTo(self.updateContent.mas_bottom).offset(24);

    }];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cancelBtn.mas_right).offset(16);
        make.right.equalTo(self.containView).offset(-22);
        make.width.height.centerY.equalTo(self.cancelBtn);
    }];
    
    self.checkBtn = [[UIButton alloc]init];
    [self.checkBtn addTarget:self action:@selector(checkMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.checkBtn setImage:[UIImage imageNamed:@"common_select_tag"] forState:UIControlStateNormal];
    [self.checkBtn setImage:[UIImage imageNamed:@"common_selected_tag"] forState:UIControlStateSelected];
    [self.containView addSubview:self.checkBtn];
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containView).offset(24);
        make.top.equalTo(self.cancelBtn.mas_bottom).offset(8);
        
    }];

    self.remindMessage = [[UILabel alloc]init];
    self.remindMessage.text = @"今天不再提示我";
    self.remindMessage.font = font(14);
    self.remindMessage.textColor = [UIColor lightGrayColor];
    [self.containView addSubview:self.remindMessage];
    [self.remindMessage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.checkBtn);
        make.left.equalTo(self.checkBtn.mas_right).offset(10);
        make.bottom.equalTo(self.containView.mas_bottom).offset(-24);
    }];
    
    self.forceBtn = [[UIButton alloc]init];
    [self.forceBtn setTitle:@"立即更新" forState:UIControlStateNormal];
    [self.forceBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.forceBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    self.forceBtn.titleLabel.font = font(14);
    self.forceBtn.backgroundColor = [RPAppManager themColor];
    self.forceBtn.layer.cornerRadius = 17;
    self.forceBtn.layer.masksToBounds = YES;
    self.forceBtn.hidden = YES;
    [self.containView addSubview:self.forceBtn];
    
    [self.forceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(34);
        make.left.equalTo(self.containView).offset(22);
        make.right.equalTo(self.containView).offset(-22);
        make.top.equalTo(self.updateContent.mas_bottom).offset(24);
    }];
    
    self.updateContent.text = self.versionModel.content;
    self.versionLb.text = self.versionModel.appVersion;
    // 强制升级
    if (self.versionModel.updateType.intValue == 3) {
        self.confirmBtn.hidden = YES;
        self.cancelBtn.hidden = YES;
        self.checkBtn.hidden = YES;
        self.remindMessage.hidden = YES;
        self.forceBtn.hidden = NO;
    }
    
}

- (void)checkMethod{
    // 取出老的version
    self.checkBtn.selected = !self.checkBtn.selected;
}
- (void)cancelMethod{
    [self removeFromSuperview];
    
    if (self.cancel) {
        self.cancel();
    }
    if (self.checkBtn.isSelected) {
        long time = [NSDate date].timeIntervalSince1970;
        [[NSUserDefaults standardUserDefaults]setObject:@(time) forKey:@"updateVersionTime"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
}
- (void)confirmMethod{
   
    if (self.done) {
        self.done();
    }
    if (self.checkBtn.isSelected) {
        long time = [NSDate date].timeIntervalSince1970;
        [[NSUserDefaults standardUserDefaults]setObject:@(time) forKey:@"updateVersionTime"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
    [self removeFromSuperview];
}
- (void)showInView:(UIView *)view{
    [view addSubview:self];
}


@end
